package com.g3net.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import com.g3net.database.DataBase;
import com.g3net.database.DataBaseFactory;
import com.g3net.database.DbException;
import com.g3net.database.dbpool.DBPoolFactory;
import com.g3net.tool.CachedClientFactory;
import com.g3net.tool.HttpUtils;
import com.shop.util.Config;


public class ContextListener implements ServletContextListener {
	private  Logger log = Logger.getLogger(ContextListener.class);
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		//CachedClientFactory.clearAll();
	
		try{
			CachedClientFactory.clearAll();
			DBPoolFactory.clearAll();
//			HttpUtils.shutdown();
		}catch(Exception e){
			log.error("",e);
		}
	}

	public void contextInitialized(ServletContextEvent se) {
		//初始化cache服务
		try {
			boolean isok=CachedClientFactory.regist(Config.CACHE_NAME, Config.CACHE_IP, CachedClientFactory.CachedClientType.JavaCachedClient);
			log.info("初始化cache服务：Config.CACHE_IP="+Config.CACHE_IP+" isok="+isok);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("",e);
		}

	}

	public static void main(String[] args) {
		try {
			DataBase db=DataBaseFactory.getDataBase();
			System.out.println(db.getAutoCommit());
		} catch (DbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
